<?php

$thisPage = "svprofile";
$subpage = getRequestParameter("subpage", false);

$extraparams = unserialize(stripslashes(getRequestParameter("params")));

TemplateHelper::useLayout("form");

addDataService("getlogo");
addHtmlService("getlogo");
addDataservice("save");

function buildLayout(&$smarty) {
    global $subpage;

    if ($subpage === false) {
        $smarty->assign("form", getProfileFormData($smarty));
    } else if ($subpage == "success") {
        $smarty->assign("form", getServiceProfileSuccessData($smarty));
    }
}

function executeService($serviceName, $params) {
    switch ($serviceName) {
        case "getlogo" : { return service_getcompanylogo($params); }
        case "save" : { return service_save($params); }
    }
}

function service_getcompanylogo($params) {
    Database::loadModel("files");
    $data = Database::getItem($params['fileid']);
    return asImage($data['type'], $data['content']);
}

function service_save($params) {
    Database::loadModel("companies");
    if (Auth::userHasProfile()) {
        $params['company_id'] = Auth::getProfileId();
        $profileId = Auth::getProfileId();
        Database::getModel()->removeHS4($profileId);
        Database::update($params);
        Database::getModel()->setService($profileId, $params['service_id'], $params['description']);
    } else {
        $profileId = Database::addNew($params);
        Auth::setCompanyProfileId($profileId);
        Database::loadModel("users");
        Database::getModel()->setCompanyProfileId($profileId);
        Database::getModel()->setService($profileId, $params['service_id'], $params['description']);
    }
    return serviceSuccessfulWithRedirect(array(), "svprofile", "success");
}

function getPageHeader() {
    return TemplateHelper::getDefaultPageHeader();
}

function getPageFooter() {
    return TemplateHelper::getDefaultPageFooter();
}

function getProfileFormData(&$smarty) {
    global $thisPage;
    global $extraparams;

    Log::debug("Executing function: getProfileFormData");
    if (Auth::isLoggedIn()) {
        initDatabase();
        Database::loadModel("companies");
        $companyProfile = Database::getItem(Auth::getProfileId());
        $serviceProfile = Database::getModel()->getService(Auth::getProfileId());
        if (isset($extraparams['text'])) {
            $companyProfile['companyname'] = $extraparams['text'];
        }
        $smarty->assign("companyprofile", $companyProfile);
        $smarty->assign("serviceprofile", $serviceProfile);
        return array(
            title => "Service profile",
            content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "profile_content"))
        );
    } else {
        return TemplateHelper::redirectPage("login", "", array(
            redirecturl => "index.php?choice=svprofile&params=" . urlencode(serialize(array(
                text => htmlspecialchars($extraparams['text']),
            )))
        ));
    }
}

function getServiceProfileSuccessData(&$smarty) {
    global $thisPage;
    return array(
        title => "Service profile",
        content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "svprofile_success"))
    );
}

?>
